<?
    class FormElement extends HTMLControl {
        function __construct($tag,$innerhtml = null) {
            parent::__construct($tag,$innerhtml);
        }
        function Render($return = false) {
            $this->attributes->id = $this->attributes->name;
            return parent::Render($return);
        }
    }
    class Textfield extends FormElement {
        function __construct($name,$default = null) {
            // try to get the default value
            parent::__construct("input");
            $this->attributes->type = "text";
            $this->attributes->size = 35;
            $this->attributes->name = $name;
            
            $value = ($default ? $default : SettingManager::GetRawSetting($name));
            $this->attributes->value = $value;
        }
    }
    class PasswordField extends FormElement {
        function __construct($name,$default = null) {
            // try to get the default value
            parent::__construct("input");
            $this->attributes->type = "password";
            $this->attributes->size = 35;
            $this->attributes->name = $name;
            
            $value = ($default ? $default : SettingManager::GetRawSetting($name));
            $this->attributes->value = $value;
        }
    }
    class DropdownMenu extends FormElement {
        private $_default = null;
        function __construct($name,$default = null) {
            parent::__construct("select");
            
            $this->_default = $default;
            $this->attributes->name = $name;
        }
        function AddOption($value,$label = null) {
            $label = ($label == null ? $value : $label);
            $opt = new HTMLControl("option",($label == null ? $value : $label));
            $opt->attributes->value = $value;
            
            $selectedvalue = ($this->_default ? $this->_default : SettingManager::GetRawSetting($this->attributes->name));
            if ($selectedvalue == $value) 
                $opt->attributes->selected = "selected";
            
            $this->AddChild($opt);
        }
    }
    class RadioButtons extends FormElement {
        private $_default = null;
        private $_name = null;
        
        function __construct($name,$default = null) {
            parent::__construct("div");
            
            $this->_default = $default;
            $this->_name = $name;
        }
        function AddOption($value,$text = null,$customattribs = null) {
            $radio = new HTMLControl('input');
            $radio->attributes->type = 'radio';
            $radio->attributes->name = $this->_name;
            $radio->attributes->value = $value;
            if ($customattribs)
                foreach ($customattribs as $attrib => $value) $radio->SetAttribute($attrib,$value);
            
            if ($value == $this->_default) $radio->attributes->checked = 'checked';
            
            $label = new HTMLControl('label',($text ? $text : $value));
            $label->styles->display = "block";
            $label->AddChild($radio);
            $this->AddChild($label);
        }
    }
    class Checkbox extends FormElement {
        function __construct($name,$label = null,$checked = false) {
            parent::__construct("label",$label);
            
            $checkbox = new HTMLControl("input");
            $checkbox->attributes->type = "checkbox";
            $checkbox->attributes->value = 1;
            $checkbox->attributes->name = $name;
            if ($checked) $checkbox->attributes->checked = "checked";
            
            $this->AddChild($checkbox);
        } 
    }
    class Textarea extends FormElement {
        function __construct($name,$default = null) {
            $default = ($default ? $default : SettingManager::GetRawSetting($name));
            parent::__construct("textarea",$default);
            $this->attributes->name = $name;
            $this->attributes->rows = 10;
            $this->SetAttribute('class','wide');
        }
    }
?>